<?php 
include_once '../Utilities/connect.php';
$conn=  getConnect();
mysql_set_charset('utf8',$conn);
/* @var $conn type */

function getProduct($CategoryID)
{
   $conn=  getConnect();
    $arr=array();
    $arr_add=array();
    $sql="select * from product where CategoryID='$CategoryID'";
    $result=  mysql_query($sql, $conn);
    if(!$result)
        die('Truy van ko hop le');
    $i=0;
    while($row= mysql_fetch_assoc($result))
    {
      $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image']);
      if($arr!=NULL)
        $arr_add[$i]=($arr);
      $i+=1;   
    }
    return $arr_add;
    mysql_close($conn);
}
function getProduct_ID($ProductID)
{
   $conn=  getConnect();
    $arr=array();
    $arr_add=array();
    $sql="select ProductID,ProductName,Description,Price,Size,Image,CategoryName,product.CategoryID,Discount from product,categories where product.CategoryID=categories.CategoryID and ProductID='$ProductID' ";
    $result=  mysql_query($sql, $conn);
    if(!$result)
        die('Truy van ko hop le');
    $i=0;
    while($row= mysql_fetch_assoc($result))
    {
      $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image'],6=>$row['CategoryName'],7=>$row['CategoryID'],8=>$row['Discount']);
      if($arr!=NULL)
        $arr_add[$i]=($arr);
      $i+=1;   
    }
    return $arr_add;
    mysql_close($conn);
}
function getCategoryName($CategoryID)
{
  $conn=  getConnect();
    $arr=array();
    $sql="select ProductID,ProductName,Description,Price,Size,Image,CategoryName from product,categories where product.CategoryID=categories.CategoryID and product.CategoryID='$CategoryID' ";
    $result=  mysql_query($sql, $conn);
    if(!$result)
        die('Truy van ko hop le');
    while($row= mysql_fetch_assoc($result))
    {
        $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image'],6=>$row['CategoryName']);
    }
    return $arr;
    mysql_close($link);
}
function getRecordProduct($display)
{
    $conn=  getConnect();
    $sql="select count(ProductID) from product";
    $res=  mysql_query($sql, $conn) or die('Could not query excuted');
    $rows=  mysql_fetch_array($res, MYSQL_NUM);
    $record=$rows[0];
    if($record>$display)
        $page=  ceil ($record/$display);
    else
        $page=1;
    return $page;
    mysql_close($conn);
}
function getProducts($start,$display)
{
    $conn=  getConnect();
    $arr=array();
    $arr_add=array();
    $i=0;
    $sql="select * from product limit $start,$display";
    $result=  mysql_query($sql, $conn);
    if(!$result)
        die('Truy van ko hop le');
    
    while($row= mysql_fetch_assoc($result))
    {
      $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image'],6=>$row['CategoryID'],7=>$row['Discount']);
      $arr_add[$i]=$arr;
      $i+=1;   
    }
    return $arr_add;
    mysql_close($conn);
}
function getCakes()
{
    $conn=  getConnect();
    $arr=array();
    $arr_add=array();
    $sql="SELECT ProductID, ProductName, Description,Price,Size,Image FROM `product` , categories WHERE product.CategoryID = categories.CategoryID AND ParentID =1";
    $result=  mysql_query($sql, $conn); 
    if(!$result)
        die('Truy van ko hop le');
    while($row= mysql_fetch_assoc($result))
    {
      $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image']);
      $arr_add[]=$arr;  
    }
    return $arr_add;
    mysql_close($conn);
    
}
function getFlowers()
{
    $conn=  getConnect();
    $arr=array();
    $arr_add=array();
    $sql="SELECT ProductID, ProductName, Description,Price,Size,Image FROM `product` , categories WHERE product.CategoryID = categories.CategoryID AND ParentID =2";
    $result=  mysql_query($sql, $conn); 
    if(!$result)
        die('Truy van ko hop le');
    while($row= mysql_fetch_assoc($result))
    {
      $arr=array(0=>$row['ProductID'],1=>$row['ProductName'],2=>$row['Description'],3=>$row['Price'],4=>$row['Size'],5=>$row['Image']);
      $arr_add[]=$arr;  
    }
    return $arr_add;
    mysql_close($conn);
    
}
function InsertProduct($name, $description, $image,$price,$size,$categoryID)
{
        $conn=  getConnect();
	$sql= "insert into product(ProductName, Description, Image,Price,Size,CategoryID ) values('$name','$description','$image','$price','$size','$categoryID')";
        $result=  mysql_query($sql,$conn);
        return $result;
        mysql_close($conn);
}

function DeleteProduct($productID)
{
         $conn=  getConnect();
         if(!$conn)
             die("Error connect");
	$sql= "delete from product where ProductID='$productID'";    
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die("error query");
        return $result;
        mysql_close($conn);
}
function UpdateProduct($name, $description,$price,$size,$categoryID,$image,$productID)
{
     $conn=  getConnect();
//	$sql= "update product set ProductName='$name',Description='$description',Price='$price',Size='$size',CategpryID='$categoryID' where ProductID='$productID'";
     $sql="UPDATE `product` SET `ProductName`=[$name],`Description`=[$description],`Image`=[$image],`Price`=[$price],`Size`=[$size],`CategoryID`=[$categoryID] WHERE 'ProductID'=[$productID]";
        $result=  mysql_query($sql,$conn);
        if(!$result)
            die('error query');
        return $result;
        mysql_close($conn);
}
function UpdateProductDemo($name, $description,$price,$size,$cateID,$img,$ID)
{
     $conn=  getConnect();
	$sql= "update product set ProductName='$name',Description='$description',Price='$price',Size='$size',CategoryID='$cateID',Image='$img' where ProductID='$ID'";
        $result=  mysql_query($sql,$conn);
        return $result;
        mysql_close($conn);
}
function checkProductName($productName)
{
    $conn=  getConnect();
    $sql="select * from product where ProductName='$productName'";
    $result=  mysql_query($sql, $conn);
   if(mysql_num_rows($result)!=0)
		{
		 	return true;
		}
		return false;
    mysql_close($conn);
}
?>